package com.yootk.main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.lang.reflect.Method;
import java.util.*;

class Book{
    private String name ;
    private double price ;
    public Book(String name,double price){
        this.name = name ;
        this.price = price ;
    }

    @Override
    public String toString() {
        return "书的名称：" + this.name + "，价格：" + this.price;
    }
}
public class SpELDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(SpELDemo.class) ;
    public static void main(String[] args) throws Exception{
        Map<String,String> map = new HashMap<>() ;
        map.put("s","sun")  ;
        map.put("z","zheng") ;
        map.put("x","xian") ;
        String content = "#{#var.?[#this.key.contains('s')]}" ;
        ExpressionParser parser = new SpelExpressionParser() ;
        Expression expression = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION) ;
        EvaluationContext context = new StandardEvaluationContext("yootk.com") ;
        context.setVariable("var",map);
        LOGGER.info("结果是：{}",expression.getValue(context,Map.class));
        LOGGER.info("结果是：{}",map);
    }

}
